package Test;

/**
 * 判断一个链表是否有环
 * 快慢指针 : 定义fast 和slow,从head开始
 * 当快指针不为空或者快指针的next不为空时 , 快指针走两步 , 慢指针走一步
 *当如果快指针等于慢指针时 , 说明有环 返回true . 否则返回false
 */
public class Test06 {
    public boolean hasCycle(MySingleList.ListNode head) {
        MySingleList.ListNode fast = head;
        MySingleList.ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if (fast == slow) {
                return true;
            }
        }
        return false;
    }
    public static void main(String[] args) {

    }
}
